<?php
/**
 * Created by PhpStorm.
 * User: lixy
 * Date: 2015/12/24
 * Time: 14:37
 */

namespace app\lib;


class Tools
{
    public static function curl( $url , $postFields = NULL )
    {
        $ch = curl_init();
        curl_setopt( $ch , CURLOPT_TIMEOUT , 30 );
        curl_setopt( $ch , CURLOPT_URL , $url );
        curl_setopt( $ch , CURLOPT_FAILONERROR , FALSE );
        curl_setopt( $ch , CURLOPT_RETURNTRANSFER , TRUE );
        //https 请求
        if ( strlen( $url ) > 5 && strtolower( substr( $url , 0 , 5 ) ) == 'https' ){
            curl_setopt( $ch , CURLOPT_SSL_VERIFYPEER , FALSE );
            curl_setopt( $ch , CURLOPT_SSL_VERIFYHOST , FALSE );
        }

        if ( is_array( $postFields ) && 0 < count( $postFields ) ){
            $postBodyString = '';
            $postMultipart  = FALSE;
            foreach ( $postFields as $k => $v ) {
                if ( '@' != substr( $v , 0 , 1 ) ) //判断是不是文件上传
                {
                    $postBodyString .= "$k=" . urlencode( $v ) . "&";
                } else {
                    //文件上传用multipart/form-data，否则用www-form-urlencoded
                    $postMultipart = TRUE;
                }
            }
            $postFields = trim( $postBodyString , '&' );
            unset( $k , $v );
            curl_setopt( $ch , CURLOPT_POST , TRUE );
            if ( $postMultipart ){
                curl_setopt( $ch , CURLOPT_POSTFIELDS , $postFields );
            } else {
                curl_setopt( $ch , CURLOPT_POSTFIELDS , $postFields );
            }
        }

        $reponse = curl_exec( $ch );
        curl_close( $ch );
        return $reponse;
    }


    static  function postJson($url,$data,$JSON_UNESCAPED_UNICODE=false){
       // $data = array("name" => "Hagrid", "age" => "36");
        if($JSON_UNESCAPED_UNICODE) {
            $data_string = json_encode($data, JSON_UNESCAPED_UNICODE);
        }else{
            $data_string = json_encode($data);
        }
        $ch = curl_init($url);
        //https 请求
        if ( strlen( $url ) > 5 && strtolower( substr( $url , 0 , 5 ) ) == 'https' ){
            curl_setopt( $ch , CURLOPT_SSL_VERIFYPEER , FALSE );
            curl_setopt( $ch , CURLOPT_SSL_VERIFYHOST , FALSE );
        }

        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json',
                'Content-Length: ' . strlen($data_string),
                'charset=utf-8',)

        );

        $result = curl_exec($ch);
        return $result;
    }

//
//var_dump( Tools::postJson_forNC("http://127.0.0.1:8080/rest/test.shtml",array("name" => "Hagrid", "age" => "36","zh"=>"中文")));
//
    public static  function postJson_forNC($url,$data){



        $data_string = json_encode($data);
        $result=Tools::curl($url,['params'=>$data_string]);


        if($result){
            $result=json_decode($result);
        }
        if(!$result){
            echo "接口访问失败:$url";
            die;
        }
        return $result;
    }


    static public function http_get_data($url) {

        $ch = curl_init ();
        curl_setopt ( $ch, CURLOPT_CUSTOMREQUEST, 'GET' );
        curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false );
        curl_setopt ( $ch, CURLOPT_URL, $url );
        ob_start ();
        curl_exec ( $ch );
        $return_content = ob_get_contents ();
        ob_end_clean ();

        $return_code = curl_getinfo ( $ch, CURLINFO_HTTP_CODE );
        return $return_content;
    }

//对象转数组
   static function objectToArray($e){
        $e=(array)$e;
        foreach($e as $k=>$v){
            if( gettype($v)=='resource' ) return;
            if( gettype($v)=='object' || gettype($v)=='array' )
                $e[$k]=(array)static::objectToArray($v);
        }
        return $e;
    }

    /**
     * 得到新订单号
     * @return  string
     */
  static  function build_order_no($pre='')
    {
        /* 选择一个随机的方案 */
        mt_srand((double) microtime() * 1000000);

        /* PHPALLY + 年月日小时分秒 + 6位随机数 */
        return $pre . date('YmdHis') . str_pad(mt_rand(1, 999999), 6, '0', STR_PAD_LEFT);
    }
}